package com.dang.alex.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.dang.alex.dao.daoInterface.ReceiveAddressDAO;
import com.dang.alex.model.ReceiveAddress;
import com.dang.alex.util.JdbcUtil;

public class ReceiveAddressDAOImp implements ReceiveAddressDAO {
	private final static String FIND_BY_ID="select * from d_receive_address where id=?";
	private final static String INSERT="insert into d_receive_address(user_id,receive_name,full_address,postal_code,mobile,phone) values(?,?,?,?,?,?)";
	private final static String FIND_BY_USERID="select d_receive_address.* from d_receive_address where user_id=?";
	@Override
	public ReceiveAddress findById(int id) throws Exception {
		Connection conn=null;
		PreparedStatement prep=null;
		ResultSet rs=null;
		ReceiveAddress receiveAddress=null;
		
		try {
			prep=JdbcUtil.getConnection().prepareStatement(FIND_BY_ID);
			prep.setInt(1, id);
			rs=prep.executeQuery();
			if(rs.next()){
				receiveAddress=new ReceiveAddress();
				receiveAddress.setFullAddress(rs.getString("full_address"));
				receiveAddress.setId(rs.getInt(id));
				receiveAddress.setMobile(rs.getString("mobile"));
				receiveAddress.setPhone(rs.getString("phone"));
				receiveAddress.setPostalCode(rs.getString("postal_code"));
				receiveAddress.setReceiceName(rs.getString("receice_name"));
				receiveAddress.setUserId(rs.getInt("user_id"));
				
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JdbcUtil.close(prep, conn, rs);
		}
		
		return receiveAddress;
		
	}

	@Override
	public void sava(ReceiveAddress addr) {
		PreparedStatement prep = null;
		Connection conn = null;
		
			try {
				conn = JdbcUtil.getConnection();
				prep = conn.prepareStatement(INSERT, Statement.RETURN_GENERATED_KEYS);
				prep.setInt(1, addr.getUserId());
				prep.setString(2, addr.getReceiceName());
				prep.setString(3, addr.getFullAddress());
				prep.setString(4, addr.getPostalCode());
				prep.setString(5, addr.getMobile());
				prep.setString(6, addr.getPhone());
				prep.executeUpdate();
				ResultSet rst = prep.getGeneratedKeys();
				rst.next();
				int id = rst.getInt(1);
				addr.setId(id);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		finally{
			JdbcUtil.close(prep, conn);
		}
		
	}

	public List<ReceiveAddress> findByUserId(int userId) {
		Connection conn=null;
		PreparedStatement prep=null;
		ResultSet rs=null;
		List<ReceiveAddress> receiveAddresss=new ArrayList<ReceiveAddress>();
		
		try {
			prep=JdbcUtil.getConnection().prepareStatement(FIND_BY_USERID);
			prep.setInt(1, userId);
			rs=prep.executeQuery();
			while(rs.next()){
				ReceiveAddress receiveAddress=new ReceiveAddress();
				receiveAddress=new ReceiveAddress();
				receiveAddress.setFullAddress(rs.getString("full_address"));
				receiveAddress.setId(rs.getInt("id"));
				receiveAddress.setMobile(rs.getString("mobile"));
				receiveAddress.setPhone(rs.getString("phone"));
				receiveAddress.setPostalCode(rs.getString("postal_code"));
				receiveAddress.setReceiceName(rs.getString("receive_name"));
				receiveAddress.setUserId(rs.getInt("user_id"));
				
				
				receiveAddresss.add(receiveAddress);
				
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JdbcUtil.close(prep, conn, rs);
		}
		
		
		return receiveAddresss;
	}

}
